package cn.com.callback
import groovy.json.*;
import groovy.swing.impl.DefaultAction
import groovy.util.slurpersupport.ReplacementNode

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import cn.com.callback.ServiceBackInvoke
import cn.com.elite.EliteBean;
import java.security.MessageDigest;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.jdbc.support.rowset.SqlRowSet
import com.wisdge.dataservice.Result;
import cn.com.elite.Util;
import org.apache.commons.io.IOUtils;
import com.wisdge.web.springframework.SpringContextUtil;
import javax.sql.DataSource
import org.springframework.web.context.request.ServletRequestAttributes
import org.springframework.web.context.support.WebApplicationContextUtils
import org.springframework.context.ApplicationContext

public class api_getcust_black implements ServiceBackInvoke{

	private static final Log log = LogFactory.getLog(api_getcust_black.class);

	public String doCallBack(EliteBean eliteBean,HttpServletRequest request,HttpServletResponse response) {
			ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
			JdbcTemplate jt = new JdbcTemplate((DataSource)ctx.getBean("dataSource"));   //获取数据源
	
			response.setCharacterEncoding("UTF-8");
			log.debug("-------------------------开始获取黑名单-----------------");
			String getMethod = request.getMethod();
			String callJson="";
			BufferedReader iny = null;
			String dealMessage = "";
			String dealResult = "";
	
			if (getMethod.equals("GET")) {
				callJson = request.getParameter("jsondata");
				log.debug("原始Get请求参数:"+callJson);
			} else {
				InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
				iny = new BufferedReader(reader);
				String line;
				while ((line = iny.readLine()) != null) {
					callJson += line;
				}
				log.debug("原始Post请求参数:"+callJson);
			}
	
			try{
				JSONObject obj = JSONObject.fromObject(callJson);
				def type = Util.filterForObject(obj.get("type"));
				log.debug("type:"+type);
				def phoneno = Util.filterForObject(obj.get("phoneno"));
				log.debug("phoneno:"+phoneno);
				if(isNull(phoneno)) {
					dealResult = "0";
					dealMessage = isNullMSG("phoneno", phoneno);
					return(dealMessage);
				}else {
                        try {
				//查询是否是黑名单
							
		def GetContentListSQL = eliteBean.chaxun_sql("select count(*) as Count FROM specialphone WHERE 1=1 and flag=3 and STATUS=1 and phoneno='" + phoneno + "'","dataSource");
		def Countnum =GetContentListSQL.get(0).get("Count");
		if(Countnum>=1) {
	                return "{\"code\": 1,\"message\": \"成功\",\"data\":{\"custisblack\":\"1\"}}";
				  
				}
				else {
                    return "{\"code\": 1,\"message\": \"成功\",\"data\":{\"custisblack\":\"0\"}}";
					}
                        }
						catch(Exception e) {
							log.error("接口处理异常"+e.getMessage());
							return "{\"code\": 0,\"message\": \"查询异常\",\"data\": null}";
							
						}
				}
				

			}
			catch(Exception e)
			{
				log.error("接口处理异常"+e.getMessage());
				return "{\"code\": 0,\"message\": \"查询异常\",\"data\": null}";
			}
			

	}
	// 非空校验isNull
	public static boolean isNull(String value) {

		boolean reFlag = true;
		if (value == "" || value.length() <= 0) {
			reFlag = true;
		} else {
			reFlag = false;
		}
		log.debug(reFlag);
		return reFlag;
	}

	// 非空校验isNullMSG
	public static String isNullMSG(String key, String value) {

		String reResult = "";
		if (value == "" || value.length() <= 0) {
			reResult = key + "必填字段;";
		}
		return reResult;
	}

}
